﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace CarbonExchange.WebControls
{
    public class RadGridTextBoxColumn : BaseRadGridColumn
    {
        public override Telerik.Web.UI.GridColumn Clone()
        {
            return new RadGridTextBoxColumn();
        }

        public Unit Width {  get; set; }

        public override void FillValues(System.Collections.IDictionary newValues, Telerik.Web.UI.GridEditableItem editableItem)
        {
            TextBox txt = editableItem.FindControl(GetControlIdToValidate(editableItem)) as TextBox;
            if(txt != null)
            {
                newValues.Add(DataField, txt.Text);
            }
        }

        protected override void InitalizeEditCell(System.Web.UI.WebControls.TableCell cell, int columnIndex, GridItem item)
        {
            TextBox txt = new TextBox();
            txt.ID = GetControlIdToValidate(item);
            cell.Controls.Add(txt);
            txt.Width = this.Width;
            txt.MaxLength = MaxLength;
            txt.DataBinding += new EventHandler(txt_DataBinding);
        }

        void txt_DataBinding(object sender, EventArgs e)
        {
            TextBox txt = sender as TextBox;
            object val = GetDataValue(txt.NamingContainer);

            if (val != null)
            {
                txt.Text = val.ToString();
            }
        }

        protected override string GetControlIdToValidate(GridItem item)
        {
            return string.Format("textArea_{0}_{1}", this.UniqueName, item.ItemIndex);
        }

    }
}
